package models

import (
	"encoding/json"
	"go-admin-official/guard"
	"go-admin-official/modules/database"
)

type UserBuyThemesModel struct {
	Base `json:"-"`

	Id          int64  `json:"id"`
	UserId      int64  `json:"user_id"`
	PluginId    int64  `json:"plugin_id"`
	OrderId     int64  `json:"order_id"`
	GoodId      int64  `json:"good_id"`
	ProductType int64  `json:"product_type"`
	CreatedAt   string `json:"created_at"`
	UpdatedAt   string `json:"updated_at"`
}

func UserBuyThemes() UserBuyThemesModel {
	return UserBuyThemesModel{Base: Base{Table: "user_buy_themes"}}
}

func (t UserBuyThemesModel) WithTx(tx *database.SqlTx) UserBuyThemesModel {
	t.Tx = tx
	return t
}

func (t UserBuyThemesModel) Find(id interface{}) UserBuyThemesModel {
	item, _ := database.Table(t.Table).Find(id)

	if item == nil {
		return UserBuyThemes()
	}

	return t.MapToModel(item)
}

var selectBuyThemesFields = []string{
	"mini_cover",
	"user_buy_themes.created_at",
	"user_buy_themes.product_type",
	"themes.updated_at",
	"themes.author",
	"themes.title",
	"themes.uuid",
	"themes.download_url",
	"themes.translation",
}

func (t UserBuyThemesModel) List(param guard.ThemesBuyParam) ([]map[string]interface{}, int, bool) {
	all, _ := database.Table(t.Table).
		LeftJoin("themes", t.Table+".plugin_id", "=", "themes.id").
		Select(selectBuyThemesFields...).
		Where("themes.audit_state", "=", 1).
		OrderBy("created_at", "desc").
		All()

	count := len(all)

	for index, item := range all {

		if translation, ok := item["translation"].(string); ok {
			var trans Translation
			if err := json.Unmarshal([]byte(translation), &trans); err == nil {
				item["title"] = trans.Get(param.Lang, item["title"].(string))
				item["author"] = trans.Get(param.Lang, item["author"].(string))
			}
		}

		if index == len(all)-1 {
			item["last"] = "last"
		} else {
			item["last"] = ""
		}

		if item["product_type"] == int64(0) {
			item["product"] = "基础授权"
		} else {
			item["product"] = "高级授权"
		}

		item["download_url"] = Plugin().WithDownloadURL(item["download_url"].(string)).GetDownloadURL()

		delete(item, "translation")
	}

	if param.Page*param.PageSize > count {
		all = all[(param.Page-1)*param.PageSize:]
	} else {
		all = all[(param.Page-1)*param.PageSize : param.Page*param.PageSize]
	}

	return all, count, param.Page*param.PageSize < count
}

func (t UserBuyThemesModel) Update(key string, value interface{}) (int64, error) {
	return database.Table(t.Table).
		Where("id", "=", t.Id).
		Update(database.H{
			key: value,
		})
}

func (t UserBuyThemesModel) IsEmpty() bool {
	return t.Id == int64(0)
}

func (t UserBuyThemesModel) IsNotEmpty() bool {
	return t.Id != int64(0)
}

func (t UserBuyThemesModel) MapToModel(m map[string]interface{}) UserBuyThemesModel {
	t.Id, _ = m["id"].(int64)
	t.UserId, _ = m["user_id"].(int64)
	t.PluginId, _ = m["plugin_id"].(int64)
	t.OrderId, _ = m["order_id"].(int64)
	t.GoodId, _ = m["good_id"].(int64)
	t.ProductType, _ = m["product_type"].(int64)
	t.CreatedAt, _ = m["created_at"].(string)
	t.UpdatedAt, _ = m["updated_at"].(string)
	return t
}
